package com.py.credit.controller;

import com.py.credit.common.Page;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.DeptUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 部门url接口controller
 */
@Controller
@RequestMapping(value = "/deptUrl")
public class DeptUrlController {

    @Autowired
    private DeptUrlService deptUrlService;

    /**
     * 查询url列表
     *
     * @param deptId
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/urlList.json")
    public Object urlList(@RequestParam(defaultValue = "", required = true, value = "deptId") String deptId, Page page) {
        List<LinkedHashMap> data = deptUrlService.selectUrlForPage(Long.parseLong(deptId), page);
        return new RestServiceResult(page.getTotalCount(), data);
    }

    /**
     * 新增url
     *
     * @param deptId
     * @param urlName
     * @param url
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addUrl.json")
    public Object addUrl(@RequestParam(defaultValue = "", required = true, value = "deptId") String deptId,
                         @RequestParam(defaultValue = "", required = true, value = "urlName") String urlName,
                         @RequestParam(defaultValue = "", required = true, value = "url") String url) {
        deptUrlService.insertUrl(urlName, url, Long.parseLong(deptId));
        return new RestServiceResult();
    }

    /**
     * 修改url
     *
     * @param urlName
     * @param url
     * @param status
     * @param delFlag
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyUrl.json")
    public Object modifyUrl(@RequestParam(defaultValue = "", required = true, value = "urlName") String urlName,
                            @RequestParam(defaultValue = "", required = true, value = "url") String url,
                            @RequestParam(defaultValue = "", required = true, value = "status") String status,
                            @RequestParam(defaultValue = "", required = true, value = "delFlag") String delFlag,
                            @RequestParam(defaultValue = "", required = true, value = "id") String id) {
        deptUrlService.modifyUrl(urlName, url, status, delFlag, id);
        return new RestServiceResult();
    }
}
